package com.framework.myandroid.view

import android.content.SharedPreferences
import androidx.annotation.Nullable

class SpHelper private constructor() {
    private val DEPTNAME = "deptname"
    private val MOBILE = "mobile"
    private val PROJECT_NAME = "3502_helper"
    private val TOKEN = "token"
    private val UID = "uid"
    private val USERNAME = "username"
    private val NAME = "name"
    private val SEX = "sex"
    private val POSTID = "post_id"
    var mSharedPreferences: SharedPreferences? = null



    companion object {
        val instance: SpHelper by lazy(mode = LazyThreadSafetyMode.SYNCHRONIZED) { SpHelper() }
    }


    fun putString(key: String?, value: String?): Boolean {
        val editor = mSharedPreferences!!.edit()
        editor.putString(key, value)
        return editor.commit()
    }

    fun getString(key: String?): String? {
        return mSharedPreferences!!.getString(key, "")
    }

    fun removeString(key: String?): Boolean {
        val editor = mSharedPreferences!!.edit()
        editor.remove(key)
        return editor.commit()
    }

    //boolean
    fun putBoolean(key: String?, value: Boolean): Boolean {
        val editor = mSharedPreferences!!.edit()
        editor.putBoolean(key, value)
        return editor.commit()
    }

    fun getBoolean(key: String?): Boolean {
        return mSharedPreferences!!.getBoolean(key, true)
    }

    //stringset
    fun putStringSet(
        key: String?,
        value: Set<String?>?
    ): Boolean {
        val editor = mSharedPreferences!!.edit()
        editor.putStringSet(key, value)
        return editor.commit()
    }

    @Nullable
    fun getStringSet(key: String?): Set<String?>? {
        return mSharedPreferences!!.getStringSet(key, null)
    }

    //int
    fun putInt(key: String?, value: Int): Boolean {
        val editor = mSharedPreferences!!.edit()
        editor.putInt(key, value)
        return editor.commit()
    }

    fun getInt(key: String?): Int {
        return mSharedPreferences!!.getInt(key, -1)
    }

    //long
    fun putLong(key: String?, value: Long): Boolean {
        val editor = mSharedPreferences!!.edit()
        editor.putLong(key, value)
        return editor.commit()
    }

    fun getLong(key: String?): Long {
        return mSharedPreferences!!.getLong(key, -1)
    }

    // LIST<BEAN>
    /*public boolean setListltruleEunuch(String datalist) {
        return putString(LIST_RULE_EUNUCH, datalist);
    }
    @Nullable
    public ArrayList<LtRuleBean> getListltruleEunuch() {
        return new Gson().fromJson(getString(LIST_RULE_EUNUCH),
                new TypeToken<List<LtRuleBean>>() {
                }.getType());
    }*/
    fun saveAll(map: Map<String?, Any>) {
        putString(TOKEN, map["token"].toString() + ",android")
        putString(UID, map["userid"] as String?)
    }

    fun getToken(): String? {
        return getString(TOKEN)
    }

    fun getUid(): String? {
        return getString(UID)
    }

    fun clearAll() {
        putString(TOKEN, "")
        putString(UID, "")
        putString(USERNAME, "")
        putString(NAME, "")
        putString(DEPTNAME, "")
        putString(MOBILE, "")
        putString(SEX, "")
    }

    fun saveUser(map: Map<String?, Any?>) {
        putString(USERNAME, map["username"] as String?)
        putString(NAME, map["name"] as String?)
        putString(POSTID, map["postId"] as String?)
        putString(DEPTNAME, map["deptName"] as String?)
        putString(MOBILE, map["mobile"] as String?)
        putString(SEX, map["sex"] as String?)
    }

    fun getUserName(): String? {
        return getString(USERNAME)
    }

    fun getName(): String? {
        return getString(NAME)
    }

    fun getDeptName(): String? {
        return getString(DEPTNAME)
    }

    fun getMobile(): String? {
        return getString(MOBILE)
    }

    fun getSex(): String? {
        return getString(SEX)
    }

}